package com.hua.yuan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hua.yuan.common.BaseActivity;
import com.hua.yuan.R;
import com.hua.yuan.manager.user.UserManagerModule;
import com.hua.yuan.manager.user.business.callback.IUserCallback;
import com.hua.yuan.provider.user.IUserProvider;
import com.hua.yuan.provider.user.User;
import com.hua.yuan.ui.data.LoginDataRequest;
import com.hua.yuan.userlogin.UserLoginModule;
import com.hua.yuan.util.SystemUtils;
import com.hua.yuan.util.ToastUtil;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * Created by huangyang on 2018/6/23.
 * 修改用户的密码
 */

public class ChangedPwdActivity extends BaseActivity implements View.OnClickListener,IUserCallback<User> {

    private ImageView backImage;
    private TextView titleView;

    private EditText oldPwd;
    private EditText newPwd;
    private EditText cofNewPwd;
    private TextView cofChanged;

    private final String TAG=ChangedPwdActivity.class.getName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changed_pwd);

        initView();
        initData();
    }

    private void initView() {

        backImage = findViewById(R.id.backImage);
        titleView = findViewById(R.id.titleView);

        oldPwd = findViewById(R.id.oldPwd);
        newPwd = findViewById(R.id.newPwd);
        cofNewPwd = findViewById(R.id.cofNewPwd);
        cofChanged = findViewById(R.id.cofChanged);

    }

    private void initData() {

        backImage.setOnClickListener(this);
        titleView.setText("修改密码");
        cofChanged.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.backImage:
                finish();
                break;
            case R.id.cofChanged:
                changedPwd();
                break;
        }
    }


    /**
     * 修改密码
     */
    private void changedPwd() {

        String old = oldPwd.getText().toString().trim();
        String newS = newPwd.getText().toString().trim();
        String cof = cofNewPwd.getText().toString().trim();

        if (TextUtils.isEmpty(old)) {
            ToastUtil.showShortToast("请输入原密码");
            return;
        }

        if (TextUtils.isEmpty(newS)) {
            ToastUtil.showShortToast("请输入新密码");
            return;
        }

        if (newS.length() < 6) {
            ToastUtil.showShortToast("新密码不能少于6位");
            return;
        }

        if (!newS.equals(cof)) {
            ToastUtil.showShortToast("两次输入的密码不一致");
            return;
        }
        IUserProvider userProvider=UserLoginModule.getInstance().getProvider();
        UserManagerModule.getInstance().getUserManager().updatePwd(userProvider,newS,this);
    }


    @Override
    public void onResult(@NonNull final User pUserInfo) {

        SystemUtils.isMainThread(TAG);

        if(pUserInfo==null){
            ToastUtil.showShortToast("密码修改失败");
            return;
        }
        ToastUtil.showShortToast("密码修改成功");
        startActivity(new Intent(this,LoginActivity.class));
        if(!isFinishing()){
            finish();
        }

    }

    @Override
    public void onError(final Throwable e) {
        SystemUtils.isMainThread(TAG);
        e.printStackTrace();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ToastUtil.showShortToast("密码修改失败");
            }
        });
    }
}
